package com.ruoyi.web.controller.applet;

import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.util.IOUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.applet.domain.MiniPledgeNum;
import com.ruoyi.applet.service.IMiniPledgeNumService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 质保编码Controller
 *
 * @author liucan
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/applet/num")
public class MiniPledgeNumController extends BaseController {
    @Resource
    private IMiniPledgeNumService miniPledgeNumService;

    @Resource
    private MiniLeagueController miniLeagueController;

    /**
     * 查询质保编码列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MiniPledgeNum miniPledgeNum, HttpServletRequest request) {
        startPage();
        miniPledgeNum.setToApplet(miniLeagueController.toApplet(request));
        List<MiniPledgeNum> list = miniPledgeNumService.selectMiniPledgeNumList(miniPledgeNum);
        return getDataTable(list,null);

    }

    /**
     * 获取质保编码详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(miniPledgeNumService.selectMiniPledgeNumById(id));
    }

    /**
     * 新增质保编码
     */
    @Log(title = "质保编码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniPledgeNum miniPledgeNum, HttpServletRequest request) {
        miniPledgeNum.setToApplet(miniLeagueController.toApplet(request));
        int i = miniPledgeNumService.insertMiniPledgeNum(miniPledgeNum);
//        if (i < 1){
//            return AjaxResult.error("此膜种类不可重复添加！");
//        }
        return toAjax(i);
    }

    /**
     * 修改质保编码
     */
    @Log(title = "质保编码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniPledgeNum miniPledgeNum, HttpServletRequest request) {
        miniPledgeNum.setToApplet(miniLeagueController.toApplet(request));
        return toAjax(miniPledgeNumService.updateMiniPledgeNum(miniPledgeNum));
    }

    /**
     * 删除质保编码
     */
    @Log(title = "质保编码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniPledgeNumService.deleteMiniPledgeNumByIds(ids));
    }

//    public static void main(String[] args) {
//        int number = 10;
//        int length = 5; // 期望的长度
//
//        // 使用String.format()方法进行0补位
//        String paddedNumber = String.format("QW"+"%0" + length + "d", number);
//
//        System.out.println(paddedNumber); // 输出: 00005
//    }


    /**
     * 导出质保编码列表
     */
    @Log(title = "质保编码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniPledgeNum miniPledgeNum) {
        List<MiniPledgeNum> list = miniPledgeNumService.selectMiniPledgeNumList(miniPledgeNum);
        ExcelUtil<MiniPledgeNum> util = new ExcelUtil<MiniPledgeNum>(MiniPledgeNum.class);
        util.exportExcel(response, list, "质保编码数据");
    }

    /**
     * 导入质保编码列表
     */
    @Transactional
    @Log(title = "质保编码", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<MiniPledgeNum> util = new ExcelUtil<MiniPledgeNum>(MiniPledgeNum.class);
        List<MiniPledgeNum> miniPledgeNumList = null;
        InputStream inputStream = file.getInputStream();
        String message = null;
        try {
            miniPledgeNumList = util.importExcel("质保编码", inputStream, 1);
            message = miniPledgeNumService.importUser(miniPledgeNumList, getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(inputStream);
        }
        return success(message);
    }
}
